import Public from "./CustomFunction";
export default {
	getdatalis(data) {
		const [year, present] = data.split(' ');
		if (year === Public.getNowFormatDate()) {
			return present
		} else {
			return year
		}
	},
	mailboxfilt(data) {
		console.log(JSON.parse(data));
		try {
			return JSON.parse(data).textAsHtml
		} catch (e) {
			//TODO handle the exception
			return data
		}

	},
	blanksplit(data) {
		const [txt, ] = data.split(' ');
		return txt
	},
	unitConverter(num) {
		if (!num || isNaN(num)) return 0
		num = Number(num)
		let backData
		if (Math.abs(num) > 100000000) {
			backData = {
				original: num,
				converterNum: Math.round((num + Number.EPSILON) * 100 / 100000000) / 100,
				unit: '亿'
			}
		} else if (Math.abs(num) > 10000) {
			backData = {
				original: num,
				converterNum: Math.round((num + Number.EPSILON) * 100 / 10000) / 100,
				unit: '万'
			}
		} else {
			backData = {
				original: num,
				converterNum: Math.round((num + Number.EPSILON) * 100) / 100,
				unit: ''
			}
		}
		return `${backData.converterNum}${backData.unit}`
	},
	formatPlayCount(count) {
		if (!count) return '';
			if (count > 10000000) {
				return `${Math.floor((count / 1000000) * 10) / 10}M`; // 233.2M
			}
			if (count > 1000000) {
				return `${Math.floor((count / 1000000) * 100) / 100}M`; // 2.3M
			}
			if (count > 1000) {
				return `${Math.floor((count / 1000) * 100) / 100}K`; // 233.23K
			}
			return count;
		
	},
	// 时间戳转时间
	dateFormat(time, format) {
		const t = new Date(parseInt(time));
		// 日期格式
		// h:i:s
		let gs = format ? 'Y-m-d h:i:s' : 'Y-m-d';
		// format = 'Y-m-d ',
		let year = t.getFullYear(),
			// 由于 getMonth 返回值会比正常月份小 1
			month = t.getMonth() + 1,
			day = t.getDate(),
			hours = t.getHours(),
			minutes = t.getMinutes(),
			seconds = t.getSeconds();
		const hash = {
			'Y': year,
			'm': month <= 9 ? String('0' + month) : month,
			'd': day <= 9 ? '0' + day : day,
			'h': hours <= 9 ? '0' + hours : hours,
			'i': minutes <= 9 ? '0' + minutes : minutes,
			's': seconds <= 9 ? '0' + seconds : seconds
		}
		return gs.replace(/\w/g, o => {
			return hash[o]
		})
	},
	/**
	 * @param {Object} str URL 地址
	 */
	isURL(str){
		
		if(str.startsWith('https:') || str.startsWith('http:')){
			try{
				new URL(str)
									
				return true
			}catch(e){
				return false
			}
		}
		
	}
}
